nilsfandomcom-20200213-history
Visual Basic .NET
Nils Wiki>Formal Sciences>Computer Sciences>Software Systems>Programming languages>Visual Basic .NET ---- Datatypes Dim variable As String 16bit Declares the variable as a string Dim variable As Short 16bit Can hold numbers from -30k to 30k Dim variable As UShort 16bit Can hold numbers from 0 to 65k Dim variable As Integer 32bit Can hold numbers from -2G to 2G Dim variable As UInteger 32bit Can hold numbers from 0 to 4G Dim variable As Long 64bit Can hold numbers from -9a to 9a Dim variable As ULong 64bit Can hold numbers from 0 to 18a Dim variable As Single 32bit Can hold numbers from -3.4E38 to 3.4E38 with 7 decimals (Floating Point) Dim variable As Double 64bit Can hold numbers from -1.7E08 to 1.7E08 with 14 decimals (Floating Point) Dim variable As Decimal 128bit Can hold numbers from -79E27 to 79E27 with 28 decimals and digits combined Dim variable As Byte 8bit Can hold numbers from 0 to 255 Dim variable As SByte 8bit Can hold numbers from -128 to 127 Dim variable As Char 16bit Can hold characters Dim variable As Boolean 16bit variable = true or variable = false variable =''' any number(true) or variable '''= 0 (false) Dim variable As Object 32bit Any data type can be stored in an object Special Declerations Public variable As datatype Declares a variable global for all forms (only if declared in a module) Const variable As datatype Declares a constant that can not change Structure structurename Dim variable1 As String Dim variable2 As Date Dim variable3 As Integer End Structure Dim variableA As structurename variableA.''variable1'' = "value\string" variableA.''variable2'' = "value\string" variableA.''variable3'' = "value\string" A structure let you make your own data type It must be declared at the top of forms, or in a module Dim variable As datatype =''' n'' Sets the variable immediately to equal ''n Other Commands variable ''' = “ value\word ”''' Sets the variable to value\word '''_ Brakes the line, and continue on the next line (Does not work in the middle of strings) &''' String concatenation ''radiobuttonname.Checked = True' or False Checks\Unchecks the radiobutton End Ends the execution buttonname.PerformClick() Is the way of clicking a button by code Arithmetic Operators +''' Addition '-' Subtraction '''* Multiplication /''' Division '''\ Whole number division Mod Remainder division (Rest) <> Not equal to >''' Greater than '''< Less than >= Grater than or equal to <= Less than or equal to a ^ b Raise a'' to the power of ''b Logical Operators And Or Not Xor AndAlso Between two if statements, and both must be true for the whole statement to be true. It automaticly skips the second IF, if the first one is false OrElse Same as over, but only one of the if's has to be true for the whole thing to be true Classes Imports System.Math Abs (n) Returns the absolute value of n'' '''Sign' (n) Returns -1 if n'' is less than 0 Returns 0 if ''n equals 0 Returns +1 if n'' is greater than 0 '''Exp' (n) Returns the constant e'' raised to the power of ''n Sqrt (n) Returns the square root of n'' '''Sin' (n) Returns the sine of the angle n'' The angle ''n expressed in radians Cos (n) Returns the cosine of the angle n'' The angle ''n expressed in radians Tan (n) Returns the tangent of the angle n'' The angle ''n expressed in radians Asin (n) Returns the arcsine in radians, of n'' '''Acos' (n) Returns the arccosine in radians, of n'' '''Atan' (n) Returns the arctangent in radians, of n'' Characters Assign strings to theese character to modulate your text in wanted way. '''Chr(10)' Moves the I-beam to the next line Chr(13) vbCr Moves the I-beam to the beginning of the line vbCrLf creates a new line vbTab creates a tab Date and Time Information Label1.Text = TimeString Show the current time Label1.Text = DateString Show the current date Label1.Text = Now Show the current date and time Label1.Text = Second(date) Show the current date and time Label1.Text = Minute(date) Show the current date and time Label1.Text = Hour(date) Show the current date and time Label1.Text = Weekday(date) Show the current date and time Label1.Text = Month(date) Show the current date and time Label1.Text = Year(date) Show the current date and time Organizing Form - Is simply a new window. Variables declared at the top of the forms, are only valid inside that same form. Module - Can store public variables and procedures that can be used in any form. Procedures (not related to event procedures) Function Procedures Are called by name. Values goes in, a value stored in the function name goes out. (Typically used for calculations) Creating functions with inputvariables: Function functionname(arguments eks:ByVal variable As datatype, Byref variable As datatype) As datatype function statements eks: variable1 = a-b-c variable2 = a+b-c Return variable2 +''' variable1 '''End Function To call the functions in forms: …a = 32 …b = 34 …c = 66 variable3 =''' functionname(input variables eks: a, b, c) ByVal - Will get the value from a parameter, which means that if the variable is changed in the procedure, the new variable value will not be passed back to the calling code. ByRef - Will get the value from a reference, which means that if the variable is changed in the procedure, the new variable value will be passed back to the calling code. Sub Procedures Sub Procedures are called by name. They receive arguments, and pass back modified arguments. The Sub name does NOT contain any specific value. (Typically used to receive or process inputs, display output, or set properties) '''Sub procedurename(arguments eks: ByVal variable As datatype, ByVal variable As datatype) procedure statements End Sub To call the functions in forms: …'variable1 =' "value\string" procedurename(inputvariables eks: variable1, variable2) Functions variable = InputBox("boxtext", "boxtitle")' - Prompts the user with a titled window with an inputbox. ''variable '''= MsgBox("boxtext", “boxtitle")''' - Displays a titled box with some predefined text. Conjunctions IF Structures '''If condition1 Then Statement1 executed if condition1 is true Elseif condition2 Then Statement2 executed if condition2 is true Elseif condition3 Then Statement3 executed if condition3 is true Else Statement4 executed if none of the above is true End If Select Case Structures Select Case variable Case value1 - Statement executed if value1 matches variable Case value2 - Statement executed if value2 matches variable Arrays Dim arrayname(dim1Index, dim2Indeks, ...) As datatype Where dim1Index is the upper bound of the first dimention (equals the number of elements minus 1) And dim2Index is the upper bound of second dimention (equals the number of elements minus 1) Can be compared to a table, where Dim1Index and Dim2Indeks are horizontal rows, and the declared number in between the limits are vertical columns To call and store in arrays arrayname(5'') = "''value\word" Stores “''value\word''” in an array, first dimention block number 5 To call without caring about the upper limit Ubound(arrayname) To call without caring about the lower limit Lbound(arrayname) Loops For Loop Loops as long as the counter value does not reach the end-number. For variable =''' startvalue '''To endvalue Step interval statements If condition1 Then Exit For - Ends the loop if condition1 happens Next variable - Adds one Stepvalue more to the counter value Do Loop Do While\Until Condition1 statements Loop To be sure that the Do Loop runs atleast one time use this variant. Do statements Loop While\Until condition1' ' Counters variable' += 1' - Equivalent to variable = variable + 1 Timers Show Time Add a Timer from the toolbox, set timer enabled from when you want it to start, and timer to 1000 (ms) in properties Label1.Text = Timestring Updates the label every second with the present time Time Limit Add a Timer from the toolbox, and double click it. Add a statement, and it will execute when the selected time (in properties) has elapsed. Events You can choose what happens when an event occur (called declerations), right next to the object symbol. Snippets Insert Snippet: - choose snippet from a list Making the User Select an Image from the Hard Drive to Display Add ToolStrip (Menus & Toolbars) Add PictureBox (Common Controls) Add OpenFileDialog (Dialogs) OpenFileDialog.Filter = ”Bitmaps (*.bmp)|*.bmp|Metafiles (*.wmf)|*.wmf” only bmp and wmf files will open OpenFileDialog1.ShowDialog() - opens the file browser PictureBox1.Image = System.Drawing.Image.FromFile ("c:\ …. .jpg") - show the selected picture in the picturebox (OpenFileDialog1.FileName) - Passes the complete path of the file to the program. Using Files on the Hard Drive The user decides where to save: Add Save File Dialog to the form SaveFileDialog1.ShowDialog() FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output) PrintLine(1, TextBox1.Text) FileClose(1) The user decides what to open: Add Open File Dialog to the form OpenFileDialog1.ShowDialog() FileOpen(1, OpenFileDialog1, OpenMode.Input) PrintLine(1, TextBox1.Text) FileClose(1) The program decides where to save: FileOpen(1, "C:\Users\Nils\...\Dokument.TXT", OpenMode.Output) PrintLine(1, TextBox1.Text) FileClose(1) The program streams text from a file: string = New StreamReader("C:\Users\Nils\...\Dokument.TXT") TextBox1.Text = StreamToDisplay.ReadToEnd string.Close() Filters: SaveFileDialog1.Filter = "Text files (*.txt)|*.txt" OpenFileDialog1.Filter = "Text files (*.txt)|*.txt" Listbox To show something in a listbox: ListBox1.Items.Add(writesomething) To get something from a listbox line number 2: Listbox1.Items(2) Additional Info Global variables is declared at the start at the form Other global or initial commands are declared under “Private Sub” Classes are called at the start of the program If a statement continiues at the next line, use underline _ A tempelate is a ready made code